//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "MJPickerViewAlertView.h"

static const CGFloat contentHeight = 400.0f;

@interface MJPickerViewAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentviewH;
@property (weak, nonatomic) IBOutlet UIView *contentview;
@property (weak, nonatomic) IBOutlet MJUIAlertVCBaseView *baseAlertView;
@property (weak, nonatomic) IBOutlet MJUIPickerItemAlertTopView *viewTop;
@property (weak, nonatomic) IBOutlet UIStackView *howDetectionStackView;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@end

@implementation MJPickerViewAlertView
#pragma mark - 系统

- (void)viewDidLoad {
    [super viewDidLoad];

    self.contentviewH.constant = contentHeight;
    self.baseAlertView.alertType = AlertTypeBottom;
    self.baseAlertView.contentHeight = contentHeight;
    [self.baseAlertView initContentview:self.contentview];

    MJWeakSelf
    self.viewTop.contentView.backgroundColor = UIColor.whiteColor;
    self.viewTop.titleLabel.mj_localizable = @"Select Birth Date";
    self.viewTop.titleLabelLeft.constant = 20;
    [self.viewTop.cancelButton mj_addAction:^(UIButton *button) {
        [weakSelf.baseAlertView dismiss];
    }];
    
    NSString *selectYear = @"";
    NSString *selectMonth = @"";
    if(self.date.length){
        NSArray *lab = [self.date componentsSeparatedByString:@"-"];
        selectYear = lab[0];
        selectMonth = lab[1];
    }
    //
    self.dataArray = [NSMutableArray array];
    NSInteger maxYear = [[[NSDate new] mj_dateConvertToStringByFormat:MJUIDateFormat14] integerValue];
    for (int i = 1900; i <= maxYear; i++) {
        [self.dataArray addObject:@(i).description];
        if(selectYear.length){
            if(i == [selectYear integerValue]){
                self.selectIndex = i - 1900;
            }
        }else{
            if(i == 1980){
                self.selectIndex = i - 1900;
            }
        }
    }
    self.monthArray = [NSMutableArray arrayWithArray:@[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"]];
    if(selectMonth.length){
        [self.monthArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
               if ([obj isEqualToString:selectMonth]) {
                   self.selectMonthIndex = idx;
               }
               NSLog(@"obj = %@, idx = %lu", obj, idx);
         }];
    }
    
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
    [self.pickerView reloadAllComponents];
    [self.pickerView selectRow:self.selectIndex inComponent:0 animated:false];
    [self.pickerView selectRow:self.selectMonthIndex inComponent:1 animated:false];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
 
}

- (IBAction)handleDoneButtonButtonEvent:(UIButton *)sender {

    MJWeakSelf
    [self.baseAlertView dismissCompletion:^{
        if(weakSelf.confirmBlock){
            NSString *dateString = [NSString stringWithFormat:@"%@-%@",self.dataArray[self.selectIndex],self.monthArray[self.selectMonthIndex]];
            weakSelf.confirmBlock(dateString);
        }
    }];
}

// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if(component == 0){
        return self.dataArray.count;
    }else{
        if(self.selectIndex == self.dataArray.count - 1){
            NSInteger maxMonth = [[[NSDate new] mj_dateConvertToStringByFormat:MJUIDateFormat18] integerValue];
            return maxMonth;
        }else{
            return self.monthArray.count;
        }
    }
}
//显示内容两种方式NSString，UIView；
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(component == 0){
        return self.dataArray[row];
    }else{
        return self.monthArray[row];
    }
}
//行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 55;
}
//自定义label
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *rowLabel = [[UILabel alloc]init];
    rowLabel.textAlignment = NSTextAlignmentCenter;
    rowLabel.backgroundColor = [UIColor clearColor];
    rowLabel.font = MJUIFontBoldMake(20);
    rowLabel.textColor = UIColor.mj_blackColor;
    [rowLabel sizeToFit];
    if(component == 0){
        rowLabel.text = self.dataArray[row];
    }else{
        rowLabel.text = self.monthArray[row];
    }
    return rowLabel;
}
//选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if(component == 0){
        NSLog(@"MJLog========year===============%ld",row);
        self.selectIndex = row;
        
        [pickerView reloadComponent:1];

    }else{
        NSLog(@"MJLog========month===============%ld",row);
        self.selectMonthIndex = row;
    }
}
#pragma mark - 其他



@end
